﻿Imports AutoMapper
Imports FTTK.Exceptions
Imports FTTK.Json
Imports FTTK.Utilities

Namespace FTTK.Services.Implementations.Rest
    Friend NotInheritable Class BaseServiceMapping

        Private Sub New()

        End Sub

        ''' <summary>
        ''' Defines the mapping for Automapper.
        ''' </summary>
        ''' <remarks></remarks>
        Friend Shared Sub DefineMapping()
            Mapper.CreateMap(Of JsonObject, FotoliaException)() _
             .ForMember(Function(a) a.Code, Sub(x) x.ResolveUsing(Function(a)
                                                                      If a.ContainsKey("code") Then
                                                                          Return a.Item("code").ToString()
                                                                      Else
                                                                          Return ""
                                                                      End If
                                                                  End Function)) _
             .ForMember(Function(a) a.Message, Sub(x) x.ResolveUsing(Function(a)
                                                                         If a.ContainsKey("code") Then
                                                                             Return FotoliaErrorMessages.GetMessageByErrorCode(a.Item("code").ToString())
                                                                         Else
                                                                             Return FotoliaErrorMessages.GetDefaultMessage()
                                                                         End If
                                                                     End Function))

            Mapper.CreateMap(Of JsonObject, FTTK.Services.Error)() _
             .ForMember(Function(a) a.Code, Sub(x) x.ResolveUsing(Function(a)
                                                                      If a.ContainsKey("code") Then
                                                                          Return a.Item("code").ToString()
                                                                      Else
                                                                          Return ""
                                                                      End If
                                                                  End Function)) _
             .ForMember(Function(a) a.Message, Sub(x) x.ResolveUsing(Function(a)
                                                                         If a.ContainsKey("code") Then
                                                                             Return FotoliaErrorMessages.GetMessageByErrorCode(a.Item("code").ToString())
                                                                         Else
                                                                             Return FotoliaErrorMessages.GetDefaultMessage()
                                                                         End If
                                                                     End Function))

            Mapper.CreateMap(Of JsonObject, Entities.FotoliaData)() _
             .ForMember(Function(a) a.NumberOfMedia, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("nb_media"))) _
             .ForMember(Function(a) a.Languages, Sub(x) x.MapFrom(Function(a) Mapper.Map(Of JsonObject, IEnumerable(Of Entities.Language))(a)))

            Mapper.CreateMap(Of JsonObject, IEnumerable(Of Entities.Language))() _
             .ConvertUsing(Function(a)
                               Dim jsonObj = a.GetValue(Of JsonObject)("languages")
                               If jsonObj IsNot Nothing Then
                                   Dim query = From c In jsonObj.Values.OfType(Of JsonObject)()
                                                Select Mapper.Map(Of JsonObject, Entities.Language)(c)
                                   Return query
                               Else
                                   Return Nothing
                               End If
                           End Function)

            Mapper.CreateMap(Of JsonObject, Entities.Language)() _
             .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) a.GetValue(Of Integer)("id"))) _
             .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.GetValue(Of String)("name")))



        End Sub
    End Class
End Namespace